package com.hp.attendance.dao;

import com.hp.attendance.entity.Menu;
import com.hp.attendance.entity.Role;
import com.hp.attendance.entity.RoleMenu;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class RoleDao extends BaseDAO {

    /**
     * 查询所有角色的列表
     * @return
     * @throws SQLException
     */
    public List<Role> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from role";
        List<Role> list = queryList(sql, new Object[0], new Role());
        return list;
    }


    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<Role> getLimitList(int pageNo,int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from role limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Role> list = queryList(sql, obj, new Role());
        return list;
    }

    /**
     * 查询总记录数
     * @return
     * @throws SQLException
     */
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from role";
        int count = queryCount(sql, new Object[0]);
        return count;
    }

    /**
     * 添加角色
     * @param role
     */
    public void addRole(Role role) throws SQLException, IOException, ClassNotFoundException {
        String sql ="insert into role values(null,?)";
        Object[] obj={role.getName()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 根据id查询role对象
     * @param id  role的主键id值
     * @return
     */
    public Role getRole(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from role where id=?";
        Object[] obj = {id};
        Role role = queryOne(sql, obj, new Role());
        return role;
    }

    /**
     * 修改角色
     * @param role
     */
    public void updateRole(Role role) throws SQLException, IOException, ClassNotFoundException {
        String sql ="update role set name=? where id=?";
        Object[] obj={role.getName(),role.getId()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 删除角色
     * @param id
     */
    public void delRole(int id) throws SQLException, IOException, ClassNotFoundException {
        String sql ="delete from role where id=?";
        Object[] obj ={id};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 根据角色id删除已分配的菜单
     * @param roleId
     */
    public void delRoleMenu(int roleId) throws SQLException, IOException, ClassNotFoundException {
        String sql ="delete from role_menu where r_id=?";
        Object[] obj = {roleId};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 执行菜单分配功能
     * @param listrm
     */
    public void addRoleMenu(List<RoleMenu> listrm) throws SQLException, IOException, ClassNotFoundException {
        StringBuilder sql =new StringBuilder("insert into role_menu values");
        for (int i = 0; i < listrm.size(); i++) {
            sql.append("(?,?),");
        }
        System.out.println(sql.toString());
        String sqlExe = sql.substring(0, sql.length() - 1);
        //设置参数
        Object[] obj=new Object[listrm.size()*2];
        for (int i = 0; i < listrm.size(); i++) {
            RoleMenu roleMenu = listrm.get(i);
            obj[2*i]=roleMenu.getR_id(); // 0    2   4  6
            obj[2*i+1]=roleMenu.getM_id();// 1   3   5  7
        }
        addOrUpdOrDel(sqlExe,obj);
    }

    /**
     * 根据员工id查询出所对应角色列表
     * @param empId
     * @return
     */
    public List<Role> getRoleByEmpId(int empId) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql="select * from role r " +
                " left join emp_role er on er.r_id =r.id " +
                " where er.emp_id=?";
        Object[] obj ={empId};
        List<Role> roles = queryList(sql, obj, new Role());
        return  roles;
    }
}
